package com.softserveinc.edu.oms.persistence.dao;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.softserveinc.edu.oms.persistence.TestHibernateSessionFactory;

public abstract class CleanUpDBTestCase extends TestCase {

	private Session session;
	private Transaction transaction;

	protected void cleanDB() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From OrderItem").executeUpdate();
		session.createQuery("Delete From Dimension").executeUpdate();
		session.createQuery("Delete From Product").executeUpdate();
		session.createQuery("Delete From Order").executeUpdate();
		session.createQuery("Delete From OrderStatus").executeUpdate();
		session.createQuery("Delete From User").executeUpdate();
		session.createQuery("Delete From CustomerType").executeUpdate();
		session.createQuery("Delete From Region").executeUpdate();
		session.createQuery("Delete From Role").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanUser() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From User").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanRole() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From Role").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanRegion() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From Region").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanProduct() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From Product").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanOrderStatus() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From OrderStatus").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanOrderItem() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From OrderItem").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanOrder() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From Order").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanDimension() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From Dimension").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void cleanCustomerType() {
		openSessionAndBeginTransaction();

		session.createQuery("Delete From CustomerType").executeUpdate();

		commitTransactionAndCloseSession();
	}

	protected void openSessionAndBeginTransaction() {
		session = TestHibernateSessionFactory.getSessionFactory().openSession();
		transaction = session.beginTransaction();
	}

	protected void commitTransactionAndCloseSession() {
		transaction.commit();
		session.close();
	}
}
